/*
 * @Author: your name
 * @Date: 2021-06-11 16:05:12
 * @LastEditTime: 2022-08-19 15:27:41
 * @LastEditors: Please set LastEditors
 * @Description: 设置空间标注的协议内容,和画布数据转换到视频页面
 * @FilePath: \web_local_rtc\src\assets\arLable\arLableData.js
 */
/**
 * 空间发送body内容设置 还有删除全部和删除某一个
 * 分为 2种 开始 和 结束
 * lableType 暂定 只有 箭头 Arrow
 * // 协议内容
 * {
  "markState" : "Start", // Start | End | DeleteAll | DeleteOne 准备开始,准备结束,删除全部，删除一个
  "arrows" : [],  // 划线坐标 [{start:{x:0,y:0},end:{x:0.5,y:0.5}}]
  "arLableType" : "Arrow", // 选择类型 Arrow（箭头），Point(点)
  "arColor":"", // 选择颜色
  "worldPosition" : { "x": 0, "y": 0, "z": 0 },
  "worldRotation" : { "x": 0, "y": 0, "z": 0, "w": 0 }
  }
 */
class ARLableData {
	worldPosition = {}
	worldRotation = {}

	/**
	 * 空间发送body内容设置 
	 * @param {string} state 'Start End DeleteAll | DeleteOne' 开始或者结束 DeleteAll 删除全部  DeleteOne删除一个
	 * @param {string} lableType Arrow 箭头 Point(点)
	 * @param {Array} arrowArr 标注坐标
	 */
	setARLable(state, xend, yend, jenx, jeny, arrowArr, arColor, obj) {
		console.log(state, xend, yend, jenx, jeny)
		let ent = sessionStorage.getItem('type')
		console.log(JSON.parse(sessionStorage.getItem('dataRominData')),'获取的数据')
		let showData = JSON.parse(sessionStorage.getItem('dataRominData'))
		console.log(ent,'setARLable---type')
		if (ent == 'anyLineData') {
			ent = 'LINE'
		} else if (ent == 'rectData') {
			ent = 'SQUARE'
		} else if (ent == 'acrData') {
			ent = 'CIRCLE'
		} else if (ent == 'textData') {
			ent = 'ARLABLETEXT'
		} else if (ent == 'arcFill' || !ent) {
			ent = 'POINT'
		} else if (ent == 'linesData') {
			ent = 'ARROW'
		}
		console.log(ent)
		let result = {}
		if (state === 'ARLABLE_START') {
			result = {
				imSendType: 'SINGLE',
				imReceiver: {
					userName: showData.callName,
					companyId: showData.companyId,
					phone: showData.callPhone,
				},
				imSender: {
					userName: sessionStorage.getItem('USER_NAME'),
					companyId: sessionStorage.getItem('companyId'),
					phone: sessionStorage.getItem('phone')
				},
				imMessageType: {
					imMessageBusinessType: 'ARLABLE',
					imMessageSpecificType: state,
				},
				imMessageBody: {
					contype: ent === 'setARLables' ? 'ARROW' : ent,
					arLableData: {
						data: [{
							x: xend,
							y: yend
						}],
						markAttribute: {
							color: sessionStorage.getItem('color'),
							radius: 0
						}
					}
				}

			}
		} else if (state === 'ARLABLE_CLOSE') {
			result = {
				imSendType: 'SINGLE',
				imReceiver: {
					userName: showData.callName,
					companyId: showData.companyId,
					phone: showData.callPhone,
				},
				imSender: {
					userName: sessionStorage.getItem('USER_NAME'),
					companyId: sessionStorage.getItem('companyId'),
					phone: sessionStorage.getItem('phone')
				},
				imMessageType: {
					imMessageBusinessType: 'ARLABLE',
					imMessageSpecificType: state,
				},
				imMessageBody: {
					contype: ent === 'setARLables' ? 'ARROW' : ent,
					arLableData: {
						data: [{
							x: xend,
							y: yend
						}, {
							x: jenx,
							y: jeny
						}],
						markAttribute: {
							color: sessionStorage.getItem('color'),
							radius: 0,
							text: ''
						}
					},
				}
			}
		} else if (state === 'ARLABLE_BACK') {
			result = {
				imSendType: 'SINGLE',
				imReceiver: {
					userName: showData.callName,
					companyId: showData.companyId,
					phone: showData.callPhone,
				},
				imSender: {
					userName: sessionStorage.getItem('USER_NAME'),
					companyId: sessionStorage.getItem('companyId'),
					phone: sessionStorage.getItem('phone')
				},
				imMessageType: {
					imMessageBusinessType: 'ARLABLE',
					imMessageSpecificType: state,
				},
				imMessageBody: {
					contype: ent,
					arLableData: {
						data: [{
							x: xend,
							y: yend
						}, {
							x: jenx,
							y: jeny,
						}],
						markAttribute: {
							color: sessionStorage.getItem('color'),
							radius: 0
						}
					},
				}
			}
		} else if (state === 'ARLABLE_CLEAR') {
			result = {
				imSendType: 'SINGLE',
				imReceiver: {
					userName: showData.callName,
					companyId: showData.companyId,
					phone: showData.callPhone,
				},
				imSender: {
					userName: sessionStorage.getItem('USER_NAME'),
					companyId: sessionStorage.getItem('companyId'),
					phone: sessionStorage.getItem('phone')
				},
				imMessageType: {
					imMessageBusinessType: 'ARLABLE',
					imMessageSpecificType: state,
				},
			}
		}

		return result
	}

	setARLables(state, xend, yend, jenx, jeny, flag) {
		let showData = JSON.parse(sessionStorage.getItem('userData'))
		console.log(state, xend, yend, jenx, jeny)
		let ent = sessionStorage.getItem('type')
		console.log(ent)
		if (ent == 'anyLineData') {
			ent = 'LINE'
		} else if (ent == 'rectData') {
			ent = 'SQUARE'
		} else if (ent == 'acrData') {
			ent = 'CIRCLE'
		} else if (ent == 'textData') {
			ent = '"ARLABLETEXT"'
		} else if (ent == 'arcFill') {
			ent = 'POINT'
		}
		console.log(ent)
		let result = {}
		if (state === 'CANVAS_START') {
			result = {
				imSendType: 'SINGLE',
				imReceiver: {
					userName: showData.callName,
					companyId: showData.companyId,
					phone: showData.callPhone,
				},
				imSender: {
					userName: sessionStorage.getItem('USER_NAME'),
					companyId: sessionStorage.getItem('companyId'),
					phone: sessionStorage.getItem('phone')
				},
				imMessageType: {
					imMessageBusinessType: 'SHAREDCANVAS',
					imMessageSpecificType: state,
				},
				imMessageBody: {
					contype: ent === 'setARLables' ? 'ARROW' : ent,
					arLableData: {
						data: [{
							x: xend,
							y: yend
						}, {
							x: jenx,
							y: jeny,
						}],
						markAttribute: {
							color: sessionStorage.getItem('color'),
							radius: 0
						}
					}
				}
			}
		} else if (state === 'CANVAS_CLOSE') {
			result = {
				imSendType: 'SINGLE',
				imReceiver: {
					userName: showData.callName,
					companyId: showData.companyId,
					phone: showData.callPhone,
				},
				imSender: {
					userName: sessionStorage.getItem('USER_NAME'),
					companyId: sessionStorage.getItem('companyId'),
					phone: sessionStorage.getItem('phone')
				},
				imMessageType: {
					imMessageBusinessType: 'SHAREDCANVAS',
					imMessageSpecificType: state,
				},
				imMessageBody: {
					contype: ent,
					arLableData: {
						data: [{
							x: xend,
							y: yend
						}, {
							x: jenx,
							y: jeny,
						}],
						markAttribute: {
							color: sessionStorage.getItem('color'),
							radius: 0
						}
					},
				}
			}
		} else {
			if (state === 'CANCAS_CLEAR' || state === 'CANCAS_BACK') {
				result = {
					imSendType: 'SINGLE',
					imReceiver: {
						userName: showData.callName,
						companyId: showData.companyId,
						phone: showData.callPhone,
					},
					imSender: {
						userName: sessionStorage.getItem('USER_NAME'),
						companyId: sessionStorage.getItem('companyId'),
						phone: sessionStorage.getItem('phone')
					},
					imMessageType: {
						imMessageBusinessType: 'SHAREDCANVAS',
						imMessageSpecificType: state,
					},
					imMessageBody: {
						canvasData: {
							contype: ent,
							markAttribute: {
								color: sessionStorage.getItem('color'),
								radius: 0,
								fontSize: ''
							},
							data: [{
								x: xend,
								y: yend
							}, {
								x: jenx,
								y: jeny
							}]
						},
						canvasRules: {
							sendID: sessionStorage.setItem('SperId'),
							isCommonCanvas: flag,
							allowCanvasUUID: []
						},
						syncCanvasData: {}
					}
				}
			}
		}
		// result.arLableType = lableType || 'Arrow'
		// result.arColor = arColor || ''
		// result.worldPosition = this.worldPosition
		// result.worldRotation = this.worldRotation
		return result
	}
	/**
	 * 留住 空间坐标返回值 用于二次发送使用
	 * @param {*} po 
	 * @param {*} ro 
	 */
	setARWorld(po, ro) {
		this.worldPosition = po || {
			x: 0,
			y: 0,
			z: 0
		}
		this.worldRotation = ro || {
			x: 0,
			y: 0,
			z: 0,
			w: 0
		}
	}
	/**
	 * 处理对应点的比例坐标,换算后再发送
	 * @param {*} pointObj  {x1:0,x2:0,y1:0,y2:0} // 坐标点击
	 * @param {*} WAndHObj {w:0,h:0} // 画布宽高
	 * @param {*} removeObj {sw:0,sh:0}
	 */
	canvasPositionToVideoPosition(pointObj, WAndHObj, removeObj) {
		let o = {
			point: pointObj,
			wh: WAndHObj,
			Rwh: removeObj
		}
		return this.dealARScale(o)
	}

	/**
	 * 处理画布到视频的比例换算
	 * 目的是将画布中点的位置的比例 换算到视频比例中
	 * 
	 * @param {*} obj 
	 */
	dealARScale(obj) {
		let result = {}

		let Sh, Sw, Sy, Sx
		let videoW = obj.wh.w
		let videoH = obj.wh.h
		const remoteWH = obj.Rwh.sw / obj.Rwh.sh
		const videoWH = videoW / videoH

		if (remoteWH > videoWH) {
			Sh = videoH
			Sw = videoH * remoteWH
			Sx = (Sw - videoW) / 2
			Sy = 0
		} else {
			Sh = remoteWH > 0 ? videoW / remoteWH : 0
			Sw = videoW // 屏幕宽度
			Sy = (Sh - videoH) / 2
			Sx = 0
		}

		result.start = {
			x: (obj.point.x1 + Sx) / Sw,
			y: Sh > 0 ? (1 - (obj.point.y1 + Sy) / Sh) : 0
		}

		result.end = {
			x: (obj.point.x2 + Sx) / Sw,
			y: Sh > 0 ? (1 - (obj.point.y2 + Sy) / Sh) : 0
		}

		return result
	}

}

export default new ARLableData()